package com.joven.shelltest.controller;

import com.joven.shelltest.utils.annostrategy.MessageConfig;
import com.joven.shelltest.utils.annostrategy.MessageDealType;
import com.joven.shelltest.utils.annostrategy.MessageHandler;
import com.joven.shelltest.utils.mapstrategy.CalculationService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * 策略模式
 * @Author: wzj
 * @Date: 2024/3/4 10:53
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/strategy")
public class TestStrategyController {

    private final CalculationService calculationService;

    @GetMapping("/test/{operation}/{num1}/{num2}")
    public int testCalculation(@PathVariable String operation, @PathVariable  int num1, @PathVariable int num2) {
        // 省略参数判空
        return calculationService.operateByStrategy(operation, num1, num2);
    }

    private final MessageConfig messageConfig;

    @GetMapping("/test/annoDrive")
    public String testAnnoDrive(String msg) {
        MessageHandler handler = null;
        for (MessageDealType dealType : MessageDealType.values()) {
            System.out.println(dealType.name());
            if (dealType.name().equals(msg)) {
                handler =  messageConfig.getMessageHandler(dealType);
            }
        }
        if (Objects.nonNull(handler)) {
            return handler.sendMessage(msg);
        }
        return "error";
    }

}
